<?php


namespace app\common\controller;
use app\common\model\Account;
use app\common\service\getRedis;
use app\lib\exception\ParameterException;

class Redis
{
    public function allKeys()
    {
        $service = new getRedis();
        $keys = $service->getAllKeys();
        return json($keys);
    }

    public function get_string($key=false)
    {
        if(!$key){
            throw new ParameterException([
                'msg' => 'key不能为空'
            ]);
        }
        $service = new getRedis();
        $value = $service->getValue($key);
        return $value;
    }

    public function get_hash($key)
    {
        if(!$key){
            throw new ParameterException([
                'msg' => 'key不能为空'
            ]);
        }
        $service = new getRedis();
        $value = $service->getAllHash($key);
        return json($value);
    }

    public function get_set($key)
    {
        if(!$key){
            throw new ParameterException([
                'msg' => 'key不能为空'
            ]);
        }
        $service = new getRedis();
        $value = $service->getAllSet($key);
        return json($value);
    }

    public function del_key($key)
    {
        $service = new getRedis();
        $value = $service->delKey($key);
        if($value){
            return json([
                'success' => true
            ]);
        }
        return json([
            'success' => false
        ]);
    }
    # 把所有的账号都添加到redis中
    public function addAccount($key, $name)
    {
        $model = new Account();
        $info_arr = $model->addAllAccount($name);
        \Driver\Cache\Redis::hMset($key, $info_arr);
    }

    # 获取所有的list

    public function get_list($key)
    {
        if(!$key){
            throw new ParameterException([
                'msg' => 'key不能为空'
            ]);
        }
        $service = new getRedis();
        $value = $service->getAllList($key);
        if($value){
            return json([
                'success' => $value
            ]);
        }
        return json([
            'success' => false
        ]);
    }
}